from flask import Blueprint, request
# from service.comment_select_service import CommentSelectService
from utils.response_utils import ResponseUtils
from werkzeug.utils import secure_filename
import os
from utils.time_utils import TimeUtils
from service.oss_service import OssService
from werkzeug.datastructures import FileStorage
from io import BytesIO
from app.service.img_service import ImgService

# 创建 Blueprint 实例
oss = Blueprint('oss', __name__)

@oss.route('/upload_slider_photo', methods=['POST'])
def upload_slider_photo():
    # Check if the 'file' part is in the request
    if 'file' not in request.files:
        return ResponseUtils.error("Please upload a file")

    # Get the file object
    file = request.files['file']

    try:
        # Upload the file to OSS
        path = OssService().aliyun_upload_file(file)
        if 'error' in path:
            return ResponseUtils.error(path['error'])

        slider_id = ImgService().insert_slider_path(path['file_url'])
        if 'error' in slider_id:
            return ResponseUtils.error(slider_id['error'])

        return ResponseUtils.success({
            "file_url": path['file_url'],
            "slider_img_id": slider_id['unique_id']
        })

    except Exception as e:
        print(f"Error in upload_slider_photo: {str(e)}")
        return ResponseUtils.error("An error occurred while processing the request")
@oss.route('/upload_note_index_photo', methods=['POST'])
def upload_note_index_photo():
    # 检查是否有文件在请求中
    if 'file' not in request.files:
        return ResponseUtils.error("Please upload a file")
    
    # 获取文件对象
    file = request.files['file']
    
    # 检查文件是否为空
    if file.filename == '':
        return ResponseUtils.error("No file selected")
    
    # 获取 note_id
    note_id = request.form.get('note_id')
    if not note_id:
        return ResponseUtils.error("Note ID is required")
    
    # 调用 OssService 进行文件上传
    try:
        path = OssService().aliyun_upload_file(file)
        print(ImgService().insert_note_index_path(path, note_id))
        return ResponseUtils.success({"file_url": path})
    except Exception as e:
        return ResponseUtils.error(str(e))
    
@oss.route('/delete_slider_photo', methods=['POST'])
def delete_slider_photo():
    try:
        unique_id = request.get_json().get('unique_id')
        res = ImgService.delete_slider_photo_by_unique_id(unique_id)
        return ResponseUtils.success(data=res)
    except Exception as e:
        return ResponseUtils.error(str(e))
    
@oss.route('/aliyun_upload_file', methods=['POST'])
def aliyun_upload_file():
    try:
        print('aliyun_upload_file',request)
        # 检查是否有文件在请求中
        if 'file' not in request.files:
            return ResponseUtils.error("Please upload a file")
        
        # 获取文件对象
        file = request.files['file']
        print('123',file)
        res = OssService.aliyun_upload_file(file)
        return ResponseUtils.success(data=res)
    except Exception as e:
        return ResponseUtils.error(str(e))